<template>
    <div id="MoodList">
        <!-- 搜索框 -->
        <div id="selectBar">
            <el-input placeholder="请输入心情关键字" v-model="searchMoodName" clearable
                style="width: 180px;margin-right: 20px;"></el-input>
            <el-button icon="el-icon-search" circle @click="search(true)" class="search"></el-button>
        </div>
        <!-- 心情表格 -->
        <el-table :data="moodData" border stripe class="el-table">
            <el-table-column prop="id" label="id" align="center"></el-table-column>
            <el-table-column label="心情图片" align="center">
                <template slot-scope="scope">
                    <el-image :src="GLOBAL.OSSImageUrlRoot + 'mood/' + scope.row.id + '.png'"
                        fit="contain" style="height: 80px;"
                        :preview-src-list="[GLOBAL.OSSImageUrlRoot + 'mood/' + scope.row.id + '.png']">
                        <div slot="error" class="image-slot">
                            <i class="el-icon-picture-outline"></i>
                        </div>
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column prop="moodName" label="心情名称" align="center"></el-table-column>
            <el-table-column label="操作" fixed="right" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" @click="currentMood = scope.row; moodEditShow = true" icon="el-icon-edit"
                        size="mini"></el-button>
                    <el-button type="primary" @click="currentMood = scope.row; moodDeleteShow = true;"
                        :disabled="defaultMood.includes(scope.row.id)" icon="el-icon-delete" size="mini"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页按钮 -->
        <div id="paging">
            <el-pagination layout="prev, pager, next" :page-size="pageSize" :total="total"
                @current-change="pageCurrentChange">
            </el-pagination>
        </div>
        <!-- 删除随笔提示框 -->
        <el-dialog title="提示" :visible.sync="moodDeleteShow" width="30%">
            <span>确定删除该心情么</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="moodDeleteShow = false">取 消</el-button>
                <el-button type="primary" @click="deleteMood(); moodDeleteShow = false;">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="编辑心情" :visible.sync="moodEditShow" width="30%">
            <el-form :model="currentMood" label-position="top">
                <el-form-item>
                    <span>id：{{ currentMood.id }}</span>
                </el-form-item>
                <el-form-item label="心情名称">
                    <el-input v-model="currentMood.moodName" clearable class="inputMoodName"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="moodEditShow = false">取 消</el-button>
                <el-button type="primary" @click="editMood(); moodEditShow = false">提 交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            moodData: [], // 心情数据
            currentMood: {}, // 当前的心情数据
            currentPage: 1, // 当前页
            pageSize: 10, // 页大小
            total: 0, // 数据总数
            searchMoodName: '', // 搜索框内容
            searchOrNot: false, // 是否搜索状态量，用于确定翻页后数据从那个方法得到
            moodEditShow: false, // 编辑心情提示框显示/隐藏
            moodDeleteShow: false, // 删除提示框显示/隐藏
            defaultMood: [1, 2, 3, 4, 5, 6, 7, 8], // 默认mood，id为1-8的表情，无法删除
        }
    },
    methods: {
        // 获取心情数据
        getAllMoodData() {
            this.$axios({
                method: 'post',
                url: '/mood/login/getAllMood/' + this.currentPage + '/' + this.pageSize
            }).then(res => {
                this.moodData = res.data.data.data
                this.total = res.data.data.total
            })
        },
        // 当element ui 的页数改变，同步页数
        pageCurrentChange(val) {
            this.currentPage = val // 更改当前页
            if (this.searchOrNot) { // 若搜索过，就用筛选搜索的方式查询数据
                this.search(false)
                return
            }
            this.getAllMoodData() // 获取数据
        },
        // 搜索心情事件
        search(buttonSearchOrNot) {
            if (buttonSearchOrNot) { // 若为按钮点击搜索,重设页状态
                this.currentPage = 1
                this.total = 0
            }
            this.searchOrNot = true // 改变搜索状态量
            // 搜索心情关键字...
            let data = new FormData()
            data.append("keywords", this.searchMoodName)
            this.$axios({
                method: 'post',
                url: '/mood/login/getMoodByKeywords/' + this.currentPage + '/' + this.pageSize,
                data: data,
                headers: {
                    'Content-Type': 'application/x-www-form-urlcoded'
                }
            }).then(res => {
                this.moodData = res.data.data.data
                this.total = res.data.data.total
            })
        },
        // 删除心情
        deleteMood() {
            this.$axios({
                method: 'post',
                url: '/mood/login/deleteMood/' + this.currentMood.id,
            }).then(res => {
                if (res.data) {
                    this.$router.go(0)
                    this.$message({
                        message: '删除成功',
                        type: 'success',
                        showClose: true,
                    })
                } else {
                    this.$message({
                        message: '删除失败！出现错误了X.X',
                        type: 'error',
                        showClose: true,
                    })
                }
            })
        },
        // 提交编辑心情事件
        editMood() {
            if (this.currentMood.moodName == null || this.currentMood.moodName == '') {
                this.$message({
                    message: '请完善信息再提交哦~',
                    type: 'error',
                    showClose: true
                })
                return
            }
            this.$axios({
                method: 'post',
                url: '/mood/login/updateMood/' + this.currentMood.id + '/' + this.currentMood.moodName,
            }).then(res => {
                if (res.data) {
                    this.$router.go(0)
                    this.$message({
                        message: '提交成功',
                        type: 'success',
                        showClose: true,
                    })
                } else {
                    this.$message({
                        message: '提交失败！出现错误了X.X',
                        type: 'error',
                        showClose: true,
                    })
                }
            })
        }
    },
    mounted() {
        this.getAllMoodData()
    }
}
</script>

<style lang="scss" scoped>
#selectBar {
    margin: 20px auto;
    display: flex;
    justify-content: center;
}
</style>
<style scoped="scoped">
/* 表格 */
.el-table {
    width: 80%;
    margin: 0 auto;
    border-radius: 20px;
}

.el-table__row {
    background-color: rgba(0, 0, 0, 0);
}

/deep/.el-table--border {
    border-color: #464646;
}

/deep/.el-table th.el-table__cell.is-leaf {
    border-color: #464646;
}

/deep/ .el-table thead tr>th {
    background-color: #202020 !important;
    color: #464646;
    border-color: #464646;
}

/deep/ .el-table tbody tr>td {
    background-color: #202020 !important;
    border-color: #464646;
}

/deep/.el-table::before,
/deep/.el-table__fixed-right::before {
    background-color: rgba(0, 0, 0, 0);
}

/deep/.el-table__empty-block {
    background-color: #202020;
}

.cell .el-button--primary {
    margin-bottom: 10px;
}
/deep/.el-table__body-wrapper {
    border-bottom: 1px solid;
}
/* 分页 */

/deep/.el-pagination {
    display: flex;
    justify-content: center;
    margin: 20px auto;
    color: #202020;
}

/deep/.el-pagination button {
    color: #fff;
    background-color: #202020;
}

/deep/ .el-pager li.active {
    color: rgb(255, 150, 50);
}

/deep/ .number {
    color: #fff;
}

/deep/ .number:hover {
    color: rgb(255, 150, 50);
}

/deep/.el-pager li {
    background-color: #202020;
}

/* 搜索Bar */
.search {
    background-color: #202020;
    color: #fff;
}

/deep/.el-input__inner {
    background-color: #202020;
}

/deep/.inputMoodName>.el-input__inner {
    background-color: #fff;
}
</style>